﻿using System.Linq;
using System.Web.Mvc;
using FinancePortal.Business.Infraestructure;
using FinancePortal.Business.Repository;
using FinancePortal.Domain.Model;
using FinancePortal.Web.Model;
using FinancePortal.Web.Models;
using System;

namespace FinancePortal.Web.Controllers
{
    [Permission(Roles = "Worker, Administrator")]
    public class ActivityController : Controller
    {
        private readonly IActivityRepository _activityRepository;
        private readonly IAreaRepository _areaRepository;
        private readonly IResourceRepository _resourceRepository;

        public ActivityController(IActivityRepository activityRepository,
            IAreaRepository areaRepository, IResourceRepository resourceRepository)
        {
            _activityRepository = activityRepository;
            _areaRepository = areaRepository;
            _resourceRepository = resourceRepository;
        }

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public JsonResult ActivityList(int jtStartIndex = 0, int jtPageSize = 0, string jtSorting = null)
        {
            try
            {
                //var ids = ResourceManagement.GetAreas(HttpContext);
                //var idArea = _resourceRepository.GetBySOEID(ResourceManagement.GetSOEID(this.Request)).AreaId;
                
                var activities = _activityRepository.GetAll() //GetByArea(idArea)
                    .OrderBy(jtSorting)
                    .Skip(jtStartIndex)
                    .Take(jtPageSize)
                    .ToList();


                var activitiesList = activities.Select(m =>
                    new ActivityViewModel
                    {
                        Id = m.Id,
                        Name = m.Name,
                        Description = m.Description,
                        AreaId = m.AreaId
                    });

                return Json(new
                {
                    Result = "OK",
                    Records = activities,
                    TotalRecordCount = _activityRepository.GetAll().Count()
                });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = ex.Message });
            }
        }

        [HttpPost]
        public JsonResult GetArea()
        {
            try
            {
                //var ids = ResourceManagement.GetAreas(HttpContext);
                //var idArea = _resourceRepository.GetBySOEID(ResourceManagement.GetSOEID(this.Request)).AreaId;
                var areas = _areaRepository.GetAll() //GetByResource(idArea)
                    .Select(m =>
                    new
                    {
                        Value = m.Id,
                        DisplayText = m.Name
                    }).ToList();

                return Json(new
                {
                    Result = "OK",
                    Options = areas
                });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = ex.Message });
            }
        }

        [HttpPost]
        public JsonResult GetParent()
        {
            try
            {
                //var ids = ResourceManagement.GetAreas(HttpContext);
                var idArea = _resourceRepository.GetBySOEID(ResourceManagement.GetSOEID(this.Request)).AreaId;
                var listActivity = _activityRepository.GetByArea(idArea).Where(m => m.ParentActivityId == null)
                    .Select(m =>
                    new
                    {
                        DisplayText = m.Name,
                        Value = m.Id
                    }).ToList();

                listActivity.Insert(0, new { DisplayText = String.Empty, Value = 0 });

                return Json(new
                {
                    Result = "OK",
                    Options = listActivity
                });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = ex.Message });
            }
        }

        [HttpPost]
        [Transactional]
        [Permission(Roles = "Administrator")]
        public JsonResult CreateActivity(ActivityViewModel view)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return Json(new
                    {
                        Result = "ERROR",
                        Message = "Form is not valid! Please correct it and try again."
                    });
                }

                var model = ActivityViewModel.ToModel(view);
                _activityRepository.Save(model);
                view.Id = model.Id;

                return Json(new
                {
                    Result = "OK",
                    Record = view
                });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = ex.Message });
            }
        }

        [HttpPost]
        [Transactional]
        [Permission(Roles = "Administrator")]
        public JsonResult UpdateActivity(ActivityViewModel view)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return Json(new
                    {
                        Result = "ERROR",
                        Message = "Form is not valid! Please correct it and try again."
                    });
                }

                _activityRepository.Save(ActivityViewModel.ToModel(view));

                return Json(new
                {
                    Result = "OK"
                });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = ex.Message });
            }
        }

        [HttpPost]
        [Transactional]
        [Permission(Roles = "Administrator")]
        public JsonResult DeleteActivity(int id)
        {
            try
            {
                var model = _activityRepository.GetById(id);
                _activityRepository.Delete(model);

                return Json(new
                {
                    Result = "OK"
                });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = ex.Message });
            }
        }

        #region Old Actions
        /*
        public ActionResult Index()
        {
            var ids = ResourceManagement.GetAreas(HttpContext);
            var list = _activityRepository.GetByArea(ids);
            return View(list);
        }

        [Permission(Roles = "Administrator")]
        public ActionResult Create()
        {
            LoadDropDown();

            return View();
        }

        [HttpPost, Transactional, Permission(Roles = "Administrator")]
        public ActionResult Create(Activity activity)
        {
            if (ModelState.IsValid)
            {
                _activityRepository.Save(activity);
                return RedirectToAction("Index");
            }

            LoadDropDown();
            return View(activity);
        }

        private void LoadDropDown()
        {
            var ids = ResourceManagement.GetAreas(HttpContext);
            var listArea = _areaRepository.GetByResource(ids);
            ViewBag.Area = new SelectList(listArea, "Id", "Name");
        }

        [Permission(Roles = "Administrator")]
        public ActionResult Edit(int id)
        {
            var activity = _activityRepository.GetById(id);

            LoadDropDown();

            return View(activity);
        }

        [HttpPost, Transactional, Permission(Roles = "Administrator")]
        public ActionResult Edit(Activity activity)
        {
            if (ModelState.IsValid)
            {
                _activityRepository.Save(activity);
                return RedirectToAction("Index");
            }

            LoadDropDown();

            return View(activity);
        }

        [Transactional, Permission(Roles = "Administrator")]
        public ActionResult Delete(int id)
        {
            var activity =_activityRepository.GetById(id);
            _activityRepository.Delete(activity);

            return RedirectToAction("Index");
        }
        */
        #endregion
    }
}
